package fr.bda.cloud.tray;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

public class CloudTrayModelList<T> extends AbstractListModel<T> {

	private static final long serialVersionUID = 1L;
	private final List<T> configurations = new ArrayList<>();

	public CloudTrayModelList() {
	}

	public CloudTrayModelList(List<T> configurations) {
		this.configurations.addAll(configurations);
	}

	@Override
	public T getElementAt(int index) {
		if (configurations.size() > index) {
			return configurations.get(index);
		}
		return null;
	}

	@Override
	public int getSize() {
		return configurations.size();
	}

	public void removeStorageUnitConfigurations(Collection<T> storageUnitConfigurationList) {
		configurations.removeAll(storageUnitConfigurationList);
		notifyChanges();
	}

	public void removeAllStorageUnitConfiguration() {
		configurations.clear();
		notifyChanges();
	}

	public void addStorageUnitConfigurations(Collection<T> storageUnitConfigurationList) {
		configurations.addAll(storageUnitConfigurationList);
		notifyChanges();
	}

	public void addStorageUnitConfiguration(T storageUnitConfiguration) {
		configurations.add(storageUnitConfiguration);
		notifyChanges();
	}

	public List<T> getAllStorageUnitConfiguration() {
		return new ArrayList<T>(configurations);
	}

	private void notifyChanges() {
		for (ListDataListener l : getListDataListeners()) {
			l.intervalAdded(new ListDataEvent(this, 0, configurations.size(), ListDataEvent.CONTENTS_CHANGED));
		}
	}
}
